package com.pi.pro.xcmg.am.was.web.xwms.controller;


import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pi.common.utils.LogSupport;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.CommonResultData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.InventoryTransferData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.TaskOrderDto;
import com.pi.pro.xcmg.am.was.fac.service.InforCommonWorkService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * XWMS-WMS接口
 */
@Slf4j
@RestController
@RequestMapping("/WebApi/api/JK/")
public class XwmsWasOrderApiController {

    protected Logger ioLogger= LogSupport.getIoLogger();

    @Autowired
    private InforCommonWorkService commonWorkService;

    private Cache<String, String> containNumCache= CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();


    @PostMapping(value="/ReceiveCKTask")
    public CommonResultData loadOrder(@RequestBody List<TaskOrderDto> tasks){
        //
        CommonResultData resultDto = InforWasExchangeSupport.ok();
        log.debug("ApiController TaskOrderApiController task receive://{}", tasks);
        ioLogger.debug(">>>> [TASK-EXEC] //{}",tasks);

        String m = this.commonWorkService.loadTaskOrder(XcmgAmConstants.DIVISION_WH101_CODE, tasks);
        if (!StringUtils.isNullOrBlank(m)){
            resultDto = InforWasExchangeSupport.error(m);
        }
        return resultDto;
    }

    @PostMapping(value="/ReceiveUpdateMsg")
    public CommonResultData transfer(@RequestBody InventoryTransferData transfer){
        //
        CommonResultData resultDto = InforWasExchangeSupport.ok();
        log.debug("ApiController InventoryApiController transfer://{}", transfer);
        ioLogger.debug(">>>> [INVENTORY-TRANSFER] //{}",transfer);

        String m = this.commonWorkService.transfer(transfer, InforWasXcmgExchangeConstants.XWMS_USER_NAME);
        if (!StringUtils.isNullOrBlank(m)){
            resultDto = InforWasExchangeSupport.error(m);
        }
        return resultDto;
    }


}
